package com.company.project.service.impl;

import com.alibaba.fastjson.JSONObject;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.company.project.common.exception.BusinessException;
import com.company.project.entity.BusiUrgentEntity;
import com.company.project.entity.SysDept;
import com.company.project.entity.SysUser;
import com.company.project.service.*;
import com.company.project.vo.resp.HomeRespVO;
import com.company.project.vo.resp.PermissionRespNode;
import com.company.project.vo.resp.UserInfoRespVO;
import org.springframework.beans.BeanUtils;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.List;

/**
 * 首页
 *
 * @author zhanggm
 * @version V1.0
 * @date 2021年6月18日
 */
@Service
public class HomeServiceImpl implements HomeService {
    @Resource
    private UserService userService;
    @Resource
    private DeptService deptService;
    @Resource
    private PermissionService permissionService;
    @Resource
    private BusiUrgentService busiUrgentService;
    @Resource
    private BusiPatrolService busiPatrolService;
    @Resource
    private BusiAlarmService busiAlarmService;
    @Resource
    private BusiDangerService busiDangerService;

    @Override
    public HomeRespVO getHomeInfo(String userId) {


        SysUser sysUser = userService.getById(userId);
        UserInfoRespVO vo = new UserInfoRespVO();

        if (sysUser != null) {
            BeanUtils.copyProperties(sysUser, vo);
            SysDept sysDept = deptService.getById(sysUser.getDeptId());
            if (sysDept != null) {
                vo.setDeptId(sysDept.getId());
                vo.setDeptName(sysDept.getName());
            }
        }

        List<PermissionRespNode> menus = permissionService.permissionTreeList(userId);

        HomeRespVO respVO = new HomeRespVO();
        respVO.setMenus(menus);
        respVO.setUserInfo(vo);

        return respVO;
    }

    @Override
    public List<JSONObject> getWorkStatistics() throws BusinessException {
        List<JSONObject> worksta = new ArrayList<>();
        // 紧急任务统计
        JSONObject urgent = new JSONObject();
        urgent.put("modelName", "紧急任务");
        urgent.put("numb", busiUrgentService.count());
        worksta.add(urgent);
        // 巡山日志统计
        JSONObject patrol = new JSONObject();
        patrol.put("modelName", "巡山日志");
        patrol.put("numb", busiPatrolService.count());
        worksta.add(patrol);
        // 一键报警统计
        JSONObject alarm = new JSONObject();
        alarm.put("modelName", "一键报警");
        alarm.put("numb", busiAlarmService.count());
        worksta.add(alarm);
        // 隐患上报统计
        JSONObject danger = new JSONObject();
        danger.put("modelName", "隐患上报");
        danger.put("numb", busiDangerService.count());
        worksta.add(danger);
        return worksta;
    }

}
